package cn.lutu.lutu.service.impl;

import cn.lutu.lutu.mapper.AlipayOrderMapper;
import cn.lutu.lutu.pojo.entity.AlipayOrder;
import cn.lutu.lutu.service.AlipayOrderService;
import cn.lutu.lutu.utils.StringUtils;
import cn.lutu.lutu.utils.SysConstant;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;

@Service
public class AlipayOrderServiceImpl extends ServiceImpl<AlipayOrderMapper, AlipayOrder> implements AlipayOrderService {
@Autowired
private AlipayOrderMapper alipayOrderMapper;
    /**
     * 创建订单
     * @return
     */
    @Override
    public AlipayOrder createOrder() {
       //1.生成订单编号
        AlipayOrder alipayOrder =new AlipayOrder();
        alipayOrder.setOrderNo(StringUtils.createOrderNum());
        alipayOrder.setSubject("测试数据11");
        alipayOrder.setStatus(SysConstant.WAIT_BUYER_PAY);
        alipayOrder.setTotalAmount("100.01");
        alipayOrder.setCreateTime(new Timestamp(new Date().getTime()));
        alipayOrder.setUpdateTime(new Timestamp(new Date().getTime()));
        alipayOrderMapper.insert(alipayOrder);
        System.out.println(alipayOrder);
        return alipayOrder;
    }
//根据订单编号获取订单
    @Override
    public AlipayOrder getByTradeNo(String outTradeNo) {
        //定义查询条件
        QueryWrapper<AlipayOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_no",outTradeNo);
        AlipayOrder alipayOrder =  alipayOrderMapper.selectOne(queryWrapper);
        return alipayOrder;
    }
//更新订单信息
    @Override
    public void updateOrderStatus(AlipayOrder alipayOrder, String tradeSuccess) {
         alipayOrder.setStatus(tradeSuccess);
         System.out.println(tradeSuccess);
         alipayOrder.setUpdateTime(new Timestamp(new Date().getTime()));
         alipayOrderMapper.updateById(alipayOrder);
    }
}
